package com.celesea.framework.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@ApiModel(value = "分页条件")
public class Page implements Serializable {

    private static final int CAPACITY = 80;
    /**
     *
     */
    @ApiModelProperty(value = "页码", required = true)
    @NotNull
    @Min(1)
    private int current;
    /**
     *
     */
    @ApiModelProperty(value = "页大小", required = true, allowableValues = "5,10,20,50,100,200")
    @NotNull
    @Range(min = 5, max = 200)
    private int pageSize;
    /**
     * 用于确定是否需要统计数量
     */
    @ApiModelProperty(hidden = true)
    private boolean getTotalCount = true;

    public Page() {
    }

    /**
     * @param page
     * @param pageSize
     */
    public Page(int page, int pageSize) {
        this(page, pageSize, true);
    }

    /**
     * @param page
     * @param pageSize
     * @param getTotalCount
     */
    public Page(int page, int pageSize, boolean getTotalCount) {
        super();
        this.current = page;
        this.pageSize = pageSize;
        this.getTotalCount = getTotalCount;
    }

    /**
     * @return
     */
    public static Page limitOne() {
        return new Page(1, 1, false);
    }

    /**
     * @param dataBuffer
     * @return
     */
    public StringBuffer toPageString(StringBuffer dataBuffer) {
        StringBuffer buffer = new StringBuffer(CAPACITY);
        buffer.append("select * from(select t.*,rownum rn from(");
        buffer.append(dataBuffer);
        buffer.append(") t where rownum<=" + current * pageSize + ") where rn>" + (current - 1) * pageSize + " ");
        return buffer;
    }

    /**
     * @return
     */
    public int getStartRecord() {
        return (current - 1) * pageSize;
    }

    /**
     * current
     *
     * @return the current
     * @since CodingExample Ver(编码范例查看) 1.0
     */

    public int getCurrent() {
        return current;
    }

    /**
     * @param current the current to set
     */

    public void setCurrent(int current) {
        this.current = current;
    }

    /**
     * pageSize
     *
     * @return the pageSize
     * @since CodingExample Ver(编码范例查看) 1.0
     */

    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 此方法描述的是：
     *
     * @Title: Page.java
     * @Package cn.ssh.core.com.celesea.framework.dao
     * @author: 317075064@qq.com
     * @date: 2013-8-9 下午3:52:26
     * @version: 1.0
     */

    public boolean isCountPage() {
        return pageSize > 0 && getTotalCount;
    }

    /**
     * 此方法描述的是：
     *
     * @Title: Page.java
     * @Package cn.ssh.core.com.celesea.framework.dao
     * @author: 317075064@qq.com
     * @date: 2013年9月10日 上午9:32:04
     * @version: 1.0
     */

    public boolean isLimitQuery() {
        return current > 0 && pageSize >= 1;
    }

    public Pager toPager(long total) {
        Pager pager = new Pager();
        pager.setCurrent(current);
        pager.setPageSize(pageSize);
        pager.setTotal(total);
        return pager;
    }
}
